package com.aischool.api;

import java.util.List;

import com.aischool.common.model.AiArticleCat;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiArticlecatService;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

@RouteMapping(url = "/api/articlecat")
public class AiArticlecatController extends Controller {
	
	

	@Inject
	AiArticlecatService aiArticlecatService;
	
	public void getAll() {
		// 结果类
		JsonResult jr = new JsonResult();
		int type_id = getInt("type_id", 0);
		int cat_state = getInt("state");
		
		if (type_id > 0) {
			Object obj = aiArticlecatService.getAll(type_id, cat_state);
			jr.setDicKV("listdata", obj);
		}
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void getOne() {
		// 结果类
		JsonResult jr = new JsonResult();

		int type_id = getInt("type_id", 0);
		int cat_id = getInt("id", 0);
		
		List<Record> listdata = aiArticlecatService.getAll(type_id, 1);
		if (listdata!=null&&listdata.size()>0) {
			jr.setDicKV("listdata", listdata);
		}
		
		if (cat_id>0) {
			Record rd = aiArticlecatService.getFirst(cat_id);
			if (rd != null) {
				jr.setDicKV("onedata", rd);
			}
		}
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void save() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 表单转换类
		int cat_id = getParaToInt("cat_id", 0);
		
		AiArticleCat info = getModel(AiArticleCat.class, "", true);
		
		
		info.removeNullValueAttrs();
		
		boolean bf = false;
		if (cat_id>0) {
			bf = info.update();
		}else {
			bf = info.save();
		}
		
		if (bf) {
			aiArticlecatService.removeCache();
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		
		renderJson(jr.Dic);
	}
	
	public void del() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 表单转换类
		int cat_id = getParaToInt("id", 0);
		
		
		boolean bf = aiArticlecatService.del(cat_id);
		
		if (bf) {
			aiArticlecatService.removeCache();
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		
		renderJson(jr.Dic);
	}
}
